package com.hnas.common.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hnas.common.dao.ImportDataPoolDAO;
import com.hnas.common.domain.BusinessType;
import com.hnas.common.orm.ImportDataPool;
import com.hnas.common.service.dto.DTOImportDataPool;
import com.hnas.common.service.dto.search.DTOImportDataPoolSearch;
import com.hnas.common.service.mapper.ImportDataPoolMapper;
import com.hnas.core.dao.PageBean;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;

@Component("importDataPoolService")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class ImportDataPoolServiceImpl implements ImportDataPoolService {

	@Autowired
	private ImportDataPoolDAO importDataPoolDAO;
	
	@Override
	public DTOPagedList<DTOImportDataPool> search(DTOImportDataPoolSearch con) {
		DTOPagedList<DTOImportDataPool> pageList = new DTOPagedList<DTOImportDataPool>();
		List<DTOImportDataPool> list = new ArrayList<DTOImportDataPool>();
		PageBean<ImportDataPool> page = new PageBean<ImportDataPool>();
		page.setPageNo(con.getPageNo());
		page.setPageSize(con.getPageSize());
		page.setTotalCount(con.getTotalCount());
		StringBuffer sb = new StringBuffer();
		List<Object> paras = new ArrayList<Object>();
		int j = 1;
		sb.append(" and t.valid = ?" + j++);
		paras.add(true);
		if (StringUtil.isNotEmpty(con.getBusinessType())) {
			sb.append(" and t.businessType = ?" + j++);
			paras.add(con.getBusinessType().trim());
		}
		if (StringUtil.isNotEmpty(con.getOpType())) {
			sb.append(" and t.opType = ?" + j++);
			paras.add(con.getOpType().trim());
		}
		if (StringUtil.isNotEmpty(con.getCreateTime())) {
			sb.append(" and t.createTime >= ?" + j++);
			paras.add(con.getCreateTime().trim() + " 00:00:00");
		}
		if (StringUtil.isNotEmpty(con.getFinishTime())) {
			sb.append(" and t.createTime <= ?" + j++);
			paras.add(con.getFinishTime().trim() + " 23:59:59");
		}
		if (sb.length() > 0) {
			sb.delete(0, 5);
			sb.insert(0, " from ImportDataPool t where ");
		} else {
			sb.append(" from ImportDataPool t");
		}
		sb.append(" order by t.createTime desc");
		page.setQuery(sb.toString());
		page.setQueryCount("select count(t.id) " + sb);
		this.importDataPoolDAO.queryByPage(page, paras.toArray());
		pageList.setPageNo(page.getPageNo());
		pageList.setPageSize(page.getPageSize());
		pageList.setTotalCount(page.getTotalCount());
		for (ImportDataPool vo : page.getList()) {
			DTOImportDataPool dto = new DTOImportDataPool();
			dto = ImportDataPoolMapper.getInstance().mapToValue(vo);
			dto.setBusinessTypeStr(BusinessType.getBusinessTypeMap().get(dto.getBusinessType()));
			dto.setOpTypeStr(BusinessType.getOpTypeString(dto.getOpType()));
			dto.setStatusStr(BusinessType.getStatusString(dto.getStatus()));
			list.add(dto);
		}
		pageList.setList(list);
		return pageList;
	}

	@Override
	public void saveImportDataPool(DTOImportDataPool dto) {
		ImportDataPool vo = this.getImportDataPool(dto);
		if(null == vo){
			vo = new ImportDataPool();
			BeanUtils.copyProperties(dto, vo, new String[] { "id" });
			vo.setValid(true);
			this.importDataPoolDAO.persist(vo);
		}else{
			BeanUtils.copyProperties(dto, vo, new String[] { "id", "valid" });
			this.importDataPoolDAO.merge(vo);
		}
	}

	@Override
	public void disableImportDataPool(Long id) {
		ImportDataPool vo = this.importDataPoolDAO.find(id);
		vo.setValid(false);
		vo.setData1(DateUtil.dateTimeToString(new Date()));
		this.importDataPoolDAO.merge(vo);
	}

	@Override
	public void save(ImportDataPool vo) {
		vo.setValid(true);
		this.importDataPoolDAO.persist(vo);
	}

	@Override
	public List<DTOImportDataPool> getProcess(String businessType) {
		String hql = "from ImportDataPool t where t.valid = 1 and t.status = '0' and t.businessType=?1";
		List<ImportDataPool> ls = this.importDataPoolDAO.findByQuery(hql, businessType);
		if(null != ls){
			return ImportDataPoolMapper.getInstance().mapToValues(ls);
		}
		return null;
	}

	@Override
	public ImportDataPool getImportDataPool(DTOImportDataPool dto) {
		if(StringUtil.isNotEmpty(dto.getBatchNum())){
			String hql = "from ImportDataPool t where t.valid = 1 and t.batchNum=?1";
			List<ImportDataPool> ls = this.importDataPoolDAO.findByQuery(hql, dto.getBatchNum());
			if(null != ls && ls.size() > 0){
				return ls.get(0);
			}
		}
		return null;
	}

}
